import { message, notification } from 'antd';
import {
  getFundFourValueStock,
  getMakeTodayInfo,
  clear,
  makeTodayInfoByTrade,
  updateStockUpdateInfo,
  insertWorth,
  getFundFourValueOption,
  getOptionSyncLastTradeInfo,
  getTradeList,
  optionTradeInfoUpdate,
  optionClosePosition,
  getOptionCreateTodayList,
  getWorth,
  getStockHistory,
  getOptionHistory,
  getMonthWorth,
  getFundLastDayInfo,
  commitImage,
  getDateList,
} from '../../../../services/allposition_service';

export default {
  namespace: 'fourfunds',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
    options: { code: 0, message: '', list: [] },
    optionsFilters: {},
    selectDate: [],
    charts: [],
    chartsMonth: [],
  },

  effects: {
    // stocks
    *GetFundFourValueStock({ payload, callback }, { call, put }) {
      const response = yield call(getFundFourValueStock, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetMakeTodayInfo({ payload, callback }, { call, put }) {
      const response = yield call(getMakeTodayInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *Clear({ payload, callback }, { call, put }) {
      const response = yield call(clear, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *MakeTodayInfoByTrade({ payload, callback }, { call, put }) {
      const response = yield call(makeTodayInfoByTrade, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *UpdateStockUpdateInfo({ payload, callback }, { call, put }) {
      const response = yield call(updateStockUpdateInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *InsertWorth({ payload, callback }, { call, put }) {
      const response = yield call(insertWorth, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // options
    *GetFundFourValueOption({ payload, callback }, { call, put }) {
      const response = yield call(getFundFourValueOption, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetTradeList({ payload, callback }, { call, put }) {
      const response = yield call(getTradeList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetOptionSyncLastTradeInfo({ payload, callback }, { call, put }) {
      const response = yield call(getOptionSyncLastTradeInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *OptionTradeInfoUpdate({ payload, callback }, { call, put }) {
      const response = yield call(optionTradeInfoUpdate, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *OptionClosePosition({ payload, callback }, { call, put }) {
      const response = yield call(optionClosePosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetOptionCreateTodayList({ payload, callback }, { call, put }) {
      const response = yield call(getOptionCreateTodayList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // Worth
    *GetWorth({ payload, callback }, { call, put }) {
      const response = yield call(getWorth, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const list = [];
        let i = 0;
        for (const item in response.data) {
          if (item === 'spy') {
            continue;
          }
          const newData = { list: [], name: '' };
          if (item === 'sc') {
            newData.name = 'Sell Call' + '净值';
          } else if (item === 'sp') {
            newData.name = 'Sell Put' + '净值';
          } else {
            newData.name = `${item}净值`;
          }
          if (item === 'stock') {
            for (let j = 0; j < response.data[item].length; j++) {
              newData.list.push({
                x: response.data[item][j].date.split(' ')[0],
                y1: response.data[item][j].percent,
                y2: response.data.spy[j].percent,
              });
            }
          } else {
            for (let j = 0; j < response.data[item].length; j++) {
              newData.list.push({
                x: response.data[item][j].date.split(' ')[0],
                y1: response.data[item][j].percent,
              });
            }
          }
          list[i] = newData;
          i++;
        }

        yield put({
          type: 'saveCharts',
          payload: list,
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // StockHistory
    *GetStockHistory({ payload, callback }, { call, put }) {
      const response = yield call(getStockHistory, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // OptionHistory
    *GetOptionHistory({ payload, callback }, { call, put }) {
      const response = yield call(getOptionHistory, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // LastDayInfo
    *GetFundLastDayInfo({ payload, callback }, { call, put }) {
      const response = yield call(getFundLastDayInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // MonthWorth
    *GetMonthWorth({ payload, callback }, { call, put }) {
      const response = yield call(getMonthWorth, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const list = [];
        let i = 0;
        for (const item in response.data) {
          if (item === 'spy') {
            continue;
          }
          const newData = { list: [], name: '' };
          if (item === 'sc') {
            newData.name = 'Sell Call' + '月度净值';
          } else if (item === 'sp') {
            newData.name = 'Sell Put' + '月度净值';
          } else {
            newData.name = `${item}月度净值`;
          }
          if (item === 'stock') {
            for (let j = 0; j < response.data[item].length; j++) {
              newData.list.push({
                x: response.data[item][j].date.split(' ')[0],
                y1: mathService.mul(response.data[item][j].profitpercent, 100),
                y2: mathService.mul(response.data.spy[j].profitpercent, 100),
              });
            }
          } else {
            for (let j = 0; j < response.data[item].length; j++) {
              newData.list.push({
                x: response.data[item][j].date.split(' ')[0],
                y1: mathService.mul(response.data[item][j].profitpercent, 100),
              });
            }
          }
          list[i] = newData;
          i++;
        }
        yield put({
          type: 'saveChartsMonth',
          payload: list,
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // CommitImage
    *CommitImage({ payload, callback }, { call, put }) {
      const response = yield call(commitImage, payload);
      if (typeof response === 'undefined') {
        if (callback) callback(false);
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback(false);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // Summary
    *GetDateList({ payload, callback }, { call, put }) {
      const response = yield call(getDateList, payload);
      if (typeof response === 'undefined') {
        if (callback) callback(false);
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveSelectDate',
          payload: response.data,
        });
        if (callback) callback(false);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.data,
        stocksFilters: action.payload.filter,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },

    saveCharts(state, action) {
      return {
        ...state,
        charts: action.payload,
      };
    },

    saveSelectDate(state, action) {
      return {
        ...state,
        selectDate: action.payload,
      };
    },

    saveChartsMonth(state, action) {
      return {
        ...state,
        chartsMonth: action.payload,
      };
    },
  },
};
